package com.frankle.jfx.antd.components.common;

import com.frankle.jfx.antd.enums.button.AButtonSize;
import com.frankle.jfx.antd.enums.button.AntButtonType;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.scene.control.Button;

/**
 * 按钮
 */
public class AntButton extends Button {

    private StringProperty buttonType = new SimpleStringProperty();
    private StringProperty buttonSize = new SimpleStringProperty();

    public AntButton() {
        this.getStyleClass().add("a-button");

        if(buttonType == null) {
            this.setType(AntButtonType.DEFAULT);
        }
    }

    public AntButton(String text) {
        super();
        this.setText(text);
    }

    public AntButton(String text, AntButtonType type) {
        super(text);
        this.setType(type);
    }

    public AntButton(String text, AntButtonType type, AButtonSize size) {
        super(text);
        this.setType(type);
        this.setSize(size);
    }

    public void setType(AntButtonType type) {
        this.buttonType.setValue(type.getType());

        //判断是否有其他按钮类型
        ObservableList<String> styleClass = this.getStyleClass();

        boolean hasButtonType = false;
        String btnType = "";

        for (AntButtonType value : AntButtonType.values()) {
            if(styleClass.contains(String.format("a-button-%s", value.getType()))) {
                hasButtonType = true;
                btnType = value.getType();
                break;
            }
        }

        if(hasButtonType) {
            styleClass.remove(String.format("a-button-%s", btnType));
        }

        this.getStyleClass().add(String.format("a-button-%s", type.getType()));
    }

    public StringProperty getButtonType() {
        return this.buttonType;
    }

    public void setSize(AButtonSize size) {
        this.buttonSize.set(size.getSize());
        this.getStyleClass().add(String.format("a-button-%s", size.getSize()));
    }

    public StringProperty getButtonSize() {
        return this.buttonSize;
    }
}
